package com.qeegoo.b2bautozityp.moudle.detail.bean;

import com.google.gson.Gson;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.entity.HttpResult;
import com.qeegoo.b2bautozityp.injector.module.AppModule;
import com.qeegoo.b2bautozityp.moudle.home.model.CheckUpdateBean;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HongQian.Wang on 2017/10/20.
 */

public class NetHelper {

    public static void getShowDetailData(String askPriceOrderId, DataSource.CallBack<DetailBean> callBack) {
        RequestApi requestApi = AppModule.provideRequestApi(AppModule.provideRetrofit());
        Call<HttpResult<DetailBean>> call = requestApi.showDetail(askPriceOrderId);
        call.enqueue(new Callback<HttpResult<DetailBean>>() {
            @Override
            public void onResponse(Call<HttpResult<DetailBean>> call, Response<HttpResult<DetailBean>> response) {
                HttpResult<DetailBean> resultBean = response.body();
                if (resultBean.getStatus().isSuccess()) {
                    callBack.onDataLoaded(resultBean.getData());
                } else {
                    decodeRspCode();
                }

            }

            @Override
            public void onFailure(Call<HttpResult<DetailBean>> call, Throwable t) {
                if (callBack != null) {
                    callBack.onDataNotAvaiable(t.getMessage());
                }
            }
        });
    }


    public static void getQuotedPriceAdd(String askPriceOrderId, DataSource.CallBack<DetailBean> callBack) {
        RequestApi requestApi = AppModule.provideRequestApi(AppModule.provideRetrofit());
        Call<HttpResult<DetailBean>> call = requestApi.quotedPriceAdd(askPriceOrderId);
        call.enqueue(new Callback<HttpResult<DetailBean>>() {
            @Override
            public void onResponse(Call<HttpResult<DetailBean>> call, Response<HttpResult<DetailBean>> response) {
                HttpResult<DetailBean> resultBean = response.body();
                if (resultBean.getStatus().isSuccess()) {
                    callBack.onDataLoaded(resultBean.getData());
                } else {
                    decodeRspCode();
                }

            }

            @Override
            public void onFailure(Call<HttpResult<DetailBean>> call, Throwable t) {
                if (callBack != null) {

                    callBack.onDataNotAvaiable(t.getMessage());
                }
            }
        });
    }


    public static void listWareHouses(DataSource.SucceedCallBack<WareHouseBean> callBack) {
        RequestApi requestApi = AppModule.provideRequestApi(AppModule.provideRetrofit());
        Call<WareHouseBean> call = requestApi.listWareHouse();
        call.enqueue(new Callback<WareHouseBean>() {
            @Override
            public void onResponse(Call<WareHouseBean> call, Response<WareHouseBean> response) {
                WareHouseBean resultBean = response.body();
                if (resultBean.getStatus().isSuccess()) {
                    callBack.onDataLoaded(resultBean);
                } else {
                    decodeRspCode();
                }

            }

            @Override
            public void onFailure(Call<WareHouseBean> call, Throwable t) {

                if (callBack != null) {
//                    callBack.onDataNotAvaiable(R.string.data_network_error);
                }
            }
        });
    }


    public static void mobileCheckupdate(DataSource.CallBack<CheckUpdateBean> callBack) {

        RequestApi requestApi = getRetrofit().create(RequestApi.class);
        Call<CheckUpdateBean> call = requestApi.mobileCheckupdate(HttpParams.getMobileCheckupdateUrl());
        call.enqueue(new Callback<CheckUpdateBean>() {
            @Override
            public void onResponse(Call<CheckUpdateBean> call, Response<CheckUpdateBean> response) {

                CheckUpdateBean checkUpdateBean = response.body();
                if (checkUpdateBean.getStatus().isSuccess()) {
                    callBack.onDataLoaded(checkUpdateBean);
                } else {
                    decodeRspCode();
                }


            }

            @Override
            public void onFailure(Call<CheckUpdateBean> call, Throwable t) {
                       callBack.onDataNotAvaiable("网络错误！");
           }
        });

    }


    public static Retrofit getRetrofit() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Retrofit.Builder builder = new Retrofit.Builder();
        Retrofit retrofit = builder.baseUrl("http://phpapi.autozi.com/")
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .build();
        return retrofit;

    }


    private static void decodeRspCode() {
        //TODO 解析失败原因

    }

}
